#!/bin/bash
SCRIPT_ABS_FILENAME=`LC_ALL=en_US.ISO8859-1 perl -e 'use Cwd "abs_path";print abs_path(shift)' "${BASH_SOURCE[0]}"`
SCRIPT_DIR=`dirname "$SCRIPT_ABS_FILENAME"`

#set -x

#echo `pwd`
#echo BASH_SOURCE="${BASH_SOURCE[@]}"
#echo "$0" "$@"

me=`basename "$0"`   # get invocation name (not the name of the target of the link)
#echo me=$me

name="${me##*-}"
#echo name=$name

prefix=${me%-$name}
echo prefix=$prefix



#parameter 1
dsc_file="$1" && shift         # path of the dsc file
#echo dsc_file=$dsc_file

#parameter 2
component="$1" && shift        # path to .inf under edk2 directory, see components section in dsc file. Ie RefindPkg/refind/refind.inf
#echo component=$component

#parameter 3
edk2config="$1" && shift       # configuration name of edk2 build script (ie DEBUG_GCC49)
#echo edk2config=$edk2config

#parameter 4
libname="$1" && shift          # lib name generated by eclipse (use ${BuildArtifactFilePrefix}${BuildArtifactFileName} )
#echo libname=$libname

#parameter 5
edk2prefix="$1" && shift            # path "$edk2prefix" to edk2 directory
#echo edk2prefix=$edk2prefix


#computed variables
component_path="$(dirname "$component")/$(basename "$component" .inf)"
#echo component_path=$component_path

edk2project="$(get_unique_property PLATFORM_NAME "$dsc_file")"
#echo edk2project=$edk2project

edk2libname="$(get_unique_property BASE_NAME "$edk2prefix"/"$component").lib"
#echo edk2libname="$edk2libname"

outputname="${libname%.*}"           # output name
#echo outputname="$outputname"




[[ -f ./"$outputname".dll ]] && rm -f ./"$outputname".dll
[[ -f ./"$outputname".pecoff ]] && rm -f ./"$outputname".pecoff
[[ -f ./"$outputname".efi ]] && rm -f ./"$outputname".efi
[[ -f ./BOOTX64.efi ]] && rm -f ./BOOTX64.efi

awk '!/'"$edk2libname"'/' "$edk2prefix"/Build/"$edk2project"/"$edk2config"/X64/"$component_path"/OUTPUT/static_library_files.lst > "$outputname"_file_list.lst

source "$SCRIPT_DIR"/../compilation/"$prefix"-path "gcc"

"$bin_path" \
    -o "$outputname".dll -nostdlib -Wl,-n,-q,--gc-sections,-z,common-page-size=0x40,--defsym=PECOFF_HEADER_SIZE=0x240 \
    -Wl,--script="$edk2prefix"/BaseTools/Scripts/GccBase.lds,--entry,_ModuleEntryPoint,-u,_ModuleEntryPoint \
    -Wl,-Map,"$outputname".map,-melf_x86_64,--oformat=elf64-x86-64,-pie \
    -g3 -fshort-wchar -fno-builtin -fno-strict-aliasing -ffunction-sections -fdata-sections -fno-common \
    -maccumulate-outgoing-args -mno-red-zone -Wno-address -mcmodel=small -fpie -fno-asynchronous-unwind-tables -Wno-address \
    -Wl,--start-group,@"$outputname"_file_list.lst,"$edk2prefix"/Build/"$edk2project"/"$edk2config"/X64/"$component_path"/OUTPUT/AutoGen.obj,"$libname" -Wl,--end-group \


set -x

if [[ $? == 0 ]]
then
  cp -f "$outputname".dll "$outputname".debug

  source "$SCRIPT_DIR"/../compilation/"$prefix"-path "objcopy"

#[[ $? == 0 ]] && "$bin_path" --strip-unneeded -R .eh_frame "$outputname".dll
  "$bin_path" "$outputname".dll
  
#  genfw_path="/JiefLand/5.Devel/Clover/Clover-projects/Clover--CloverHackyColor--master.2/BaseTools/Source/C/GenFw/DerivedData/GenFw/Build/Products/Debug/GenFw"
  genfw_path="$edk2prefix"/BaseTools/Source/C/bin/GenFw
  
  [[ $? == 0 ]] && "$genfw_path" -e UEFI_APPLICATION -o "$outputname".efi "$outputname".dll

  build_id="$(grep -aEo "CloverBuildIdGrepTag: [^[:cntrl:]]*" < ./"$outputname".efi | sed "s/CloverBuildIdGrepTag: //")"
  [[ $? == 0 ]] && cp ./"$outputname".efi "$outputname"-"$build_id".efi
  [[ $? == 0 ]] && zip ./"$outputname"-"$build_id".zip ./"$outputname"-"$build_id".efi

#  [[ $? == 0 ]] && cp ./"$outputname".efi "/JiefLand/5.Devel/Clover/user config/Matgen84/EFI_github/CLOVER/CloverX64.efi"
#  [[ $? == 0 ]] && cp ./"$outputname".efi "/JiefLand/5.Devel/Clover/user config/mifjpn/EFI_github/CLOVER/CloverX64.efi"
#  [[ $? == 0 ]] && cp ./"$outputname".efi "/JiefLand/5.Devel/Clover/user config/iCanaro/EFI_github/CLOVER/CloverX64.efi"

  [[ $? == 0 ]] && echo link successful
else
  :
fi


